from django.contrib.auth import authenticate, login
from django.http import JsonResponse
from django.shortcuts import render

from rest_framework.generics import CreateAPIView
from rest_framework.views import APIView

from .serializers import UserSer


class UserView(CreateAPIView):
    # 注册用户,指定序列化器
    serializer_class = UserSer


class LogView(APIView):
    def post(self, request):
        # 验证用户名密码
        username = request.data.get('username')
        password = request.data.get('password')
        user = authenticate(request, username=username, password=password)
        login(request, user)
        return JsonResponse({'msg': '登录成功'})
